package com.bluedon.ism.commons.action;


import com.bluedon.ism.commons.entity.Config;
import com.bluedon.ism.commons.operationlog.LogConstantAdmin;
import com.bluedon.ism.commons.operationlog.aspect.OperateLogAnnotation;
import com.bluedon.ism.commons.service.ConfigService;
import com.bluedon.ism.commons.util.Constants;
import com.bluedon.ism.commons.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 配置表action
 */
public class ConfigAction extends BaseAdminAction {

    private static final long serialVersionUID = 6882810339519138312L;

    private static final Log logger = LogFactory.getLog(ConfigAction.class);

    @Resource
    private ConfigService configService;

    private Config k_config; //验证码开关
    private Config l_config; //日志开关

    @Transactional
    @OperateLogAnnotation(moduleName= LogConstantAdmin.SYSTEM_MANAGER,operateTypeName="参数设置",message="修改了参数设置",httpMethod="POST")
    public String cycleConfig() {
        Config k_configdb=configService.getConfigNoCache("kaptcha_switch");
        Config l_configdb=configService.getConfigNoCache("opt_log_switch");
        if (isGet()) {
            this.k_config = k_configdb;
            this.l_config = l_configdb;
            return INPUT;
        }
        if (isPost()) {
            if(k_configdb!=null&&k_config!=null&& StringUtil.isNotBlank(k_config.getValue())){
                k_configdb.setValue(k_config.getValue());
                configService.update(k_configdb);
                logger.warn("保存验证码开关：" + k_configdb.getValue());
            }

            if(l_configdb!=null&&l_config!=null&&StringUtil.isNotBlank(l_config.getValue())){
                l_configdb.setValue(l_config.getValue());
                configService.update(l_configdb);
                logger.warn("保存日志开关：" + l_configdb.getValue());
            }

            request.getSession().setAttribute(Constants.SESSION_DO_FEEDBACK,
                    "保存成功！");
            return SUCCESS;
        }
        return INPUT;
    }

    public Config getK_config() {
        return k_config;
    }

    public void setK_config(Config k_config) {
        this.k_config = k_config;
    }

    public Config getL_config() {
        return l_config;
    }

    public void setL_config(Config l_config) {
        this.l_config = l_config;
    }

    public void setConfigService(ConfigService configService) {
        this.configService = configService;
    }
    
}
